package controller;

import com.github.pagehelper.PageInfo;
import entity.Customer;
import entity.LayRequest;
import entity.MyResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CustomerService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "customer_add.jsp";
    }
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(Customer customer){
        boolean flag = customerService.insert(customer);
        return new MyResult(0);
    }
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, ModelMap modelMap){
        Customer customer = customerService.selectById(id);
        modelMap.addAttribute("customer",customer);
        return "update.jsp";
    }
    @ResponseBody
    @RequestMapping("update")
    public MyResult update(Customer customer){
        boolean flag = customerService.update(customer);
        return new MyResult(0);
    }
    @ResponseBody
    @RequestMapping("/batchDelete")
    public MyResult delete(Integer[] ids){
        boolean flag = customerService.batchDelete(ids);
        return new MyResult(0);
    }
    @ResponseBody
    @RequestMapping("/list")
    public MyResult<List<Customer>> list(LayRequest layRequest,ModelMap modelMap){
        PageInfo<Customer> customerPageInfo = customerService.selectByPage(layRequest);
        return new MyResult<>(0,customerPageInfo.getTotal(),customerPageInfo.getList());
    }

    @RequestMapping("/toSearch")
    public String toSearch(){
        return "search.jsp";
    }
    @ResponseBody
    @RequestMapping("/search")
    public MyResult<List<Customer>> search(LayRequest layRequest,ModelMap modelMap){
        PageInfo<Customer> customerPageInfo = customerService.selectByPage(layRequest);
        return new MyResult(0,customerPageInfo.getTotal(),customerPageInfo.getList());
    }
    @RequestMapping("/toTransfer")
    public String toTransfer(){
        return "transfer.jsp";
    }
    @ResponseBody
    @RequestMapping("/transfer")
    public MyResult transfer(){
        return new MyResult(0);
    }

    @ResponseBody
    @RequestMapping("/export")
    public MyResult export(Integer id, HttpServletResponse response) throws IOException {
        Customer customer = customerService.selectById(id);
        System.out.println(customer);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("客户信息表");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("客户名称");
        titleRow.createCell(1).setCellValue("客户类别");
        titleRow.createCell(2).setCellValue("客户等级");
        titleRow.createCell(3).setCellValue("客户状态");
        titleRow.createCell(4).setCellValue("客户信用度");
        titleRow.createCell(5).setCellValue("客户来源");
        titleRow.createCell(6).setCellValue("客户成熟度");
        titleRow.createCell(8).setCellValue("描述客户所在地区");

        //遍历将数据放到excel列中

            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(customer.getName());
            dataRow.createCell(1).setCellValue(customer.getType());
            dataRow.createCell(2).setCellValue(customer.getLevel());
            dataRow.createCell(3).setCellValue(customer.getStatus());
            dataRow.createCell(4).setCellValue(customer.getCredit());
            dataRow.createCell(5).setCellValue(customer.getSource());
            dataRow.createCell(6).setCellValue(customer.getMaturity());
            dataRow.createCell(7).setCellValue(customer.getArea());
            /*   // 设置下载时客户端Excel的名称  
            String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";  
               response.setContentType("application/vnd.ms-excel");  
               response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

        // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("客户名单".getBytes(),"iso-8859-1") + ".xls");


        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
        return new MyResult(0);
    }
}
